layui.use(['table', 'admin', 'ax'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;

    /**
     * Warehouse Area Management
     * 库区表管理
     */
    var BasicArea = {
        tableId: "basicAreaTable"
    };

    /**
     * 初始化表格的列
     */
    BasicArea.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '主键'},
            {field: 'areaWarehouseName', title: 'Belonging Warehouse'}, // 所属仓库
            {field: 'areaName', title: 'Area Name'}, // 库区名称
            {field: 'areaNumber', sort: true, title: 'Area Number'}, // 库区编号
            {field: 'areaMeasure', title: 'Area Size', sort: true}, // 面积
            {field: 'areaProduct', title: 'Category'}, // 品类
            {field: 'areaFunction', title: 'Area Function'}, // 库区功能
            {field: 'areaCondition', title: 'Storage Conditions'}, // 保管条件
            {field: 'bigtypes', title: 'Material Category Code'}, // 物料大类编码
            {field: 'areaType', title: 'Area Type'}, // 库区类型
            {field: 'dismantlingParts', title: 'Partial Disassembly'}, // 是否拆零
            {field: 'areaNotes', title: 'Notes'}, // 备注
            {field: 'createTime', sort: true, title: 'Creation Time'}, // 创建时间
            {align: 'center', toolbar: '#tableBar', title: 'Operations'} // 操作
        ]];
    };

    /**
     * 点击查询按钮
     */
    BasicArea.search = function () {
        var queryData = {};
        queryData['condition'] = $("#condition").val();
        queryData['areaWarehouseId'] = $("#areaWarehouseId").val();
        table.reload(BasicArea.tableId, {
            where: queryData, page: {curr: 1},
             done:function () {
            }
        });
    };

    /**
     * 弹出添加对话框
     */
    BasicArea.openAddDlg = function () {
        window.location.href = Feng.ctxPath + '/basicArea/add';
    };
    
    // Batch Assign Material Categories
    // 批量分配物料大类
    BasicArea.btnAssign = function () {
    	var checkStatus = table.checkStatus('basicAreaTable');
        data2 = checkStatus.data;
        if (data2.length>0) {
            var ids = [];
            for (var i = 0; i < data2.length; i++) {
            	ids.push(data2[i].id);
			}
            layer.open({
                type: 2,
                title: 'Batch Assign Material Categories', // 批量分配物料大类
                area: ['800px', '500px'],
                offset: '100px',
                anim: 5,
                content: Feng.ctxPath + '/basicArea/toAssignMaterial?ids=' + ids,
                end: function () {
                    table.reload(BasicArea.tableId);
                    BasicArea.search();
                }
            });
		} else {
			layer.alert('Please select at least one data record'); // 请至少选中一条数据
		}
    };

    /**
     * 导出excel按钮
     */
    BasicArea.exportExcel = function () {
        var checkRows = table.checkStatus(BasicArea.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("Please select data to export"); // 请选择要导出的数据
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    BasicArea.openEditDlg = function (data) {
        window.location.href = Feng.ctxPath + '/basicArea/edit?id=' + data.id;
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    BasicArea.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/basicArea/delete", function (data) {
                Feng.success("Deletion successful!"); // 删除成功!
                table.reload(BasicArea.tableId);
            }, function (data) {
                Feng.error("Deletion failed!" + data.responseJSON.message + "!"); // 删除失败!
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("Are you sure to delete?", operation); // 是否删除?
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + BasicArea.tableId,
        url: Feng.ctxPath + '/basicArea/list',
        page: true,
        limit: 20,
        height: "full-60",
        cellMinWidth: 100,
        toolbar: '#toolbarDemo', //开启头部工具栏，并为其绑定左侧模板
        cols: BasicArea.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        BasicArea.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        BasicArea.openAddDlg();
    });
    
    // Batch Assign Material Categories
    // 批量分配物料大类
    $('#btnAssign').click(function () {
        BasicArea.btnAssign();
    });

    // 导出excel
    $('#btnExp').click(function () {
        BasicArea.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + BasicArea.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            BasicArea.openEditDlg(data);
        } else if (layEvent === 'delete') {
            BasicArea.onDeleteItem(data);
        }
    });
});
